<template>
  <div class="container">
    <el-card >
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" :inline="true" size="small">
            <el-form-item label="学科名称">
                <el-input style="200px"
                placeholder="请输入内容"
                prefix-icon="el-icon-search"
                v-model="searchName"
                ></el-input>
            </el-form-item>
            <el-form-item style="text-align:right">
              <!-- <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="search()">搜索</el-button> -->
              <el-button icon="el-icon-thumb" type="primary" @click="search">查询</el-button>
              <el-button icon="el-icon-delete" type="danger"  @click="clear">清除</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align:right">
          <el-button type="success" size="small" @click="dialogVisible = true">
            <i class="el-icon-edit"></i> 新增学科</el-button>
             <el-button  type="danger"  @click="delremoves" size="small">一键删除</el-button>
        </el-col>
      </el-row>
      <el-alert :title="title" type="info" :closable="false" show-icon>
      </el-alert>
      <!-- 表格 -->
      <el-table :data="data">
        <el-table-column type="index" :index="indexMethod" label="序号"></el-table-column>
        <el-table-column prop="subjectName" label="学科名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="160px">
            <template slot-scope="scope">{{scope.row.addDate|parseTimeByString}}</template>
        </el-table-column>
        <el-table-column  label="前台是否显示">
          <template #default="{row}">{{row.isFrontDisplay===1?'是':'否'}}</template>
           </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录"> </el-table-column>
        <el-table-column prop="tags" label="标签" width="120"> </el-table-column>
        <el-table-column prop="totals" label="题目数量" width="120"> </el-table-column>

        <el-table-column  label="操作" width="240px">
         <template #default="{row}">
          <el-button type="text" @click="$router.push(`directorys?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科分类</el-button>
          <el-button type="text" @click="$router.push(`tags?id=${row.id}&name=${encodeURIComponent(row.subjectName)}`)">学科标签</el-button>
          <el-button type="text" @click="amend(row.id)">修改</el-button>
          <el-button type="text" @click="delremove(row.id)">删除</el-button>

           </template>
         </el-table-column>
      </el-table>

      <!-- 分页器 -->

    <el-pagination
     background
     style="margin-top:20px;text-align:right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="this.getquery.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="this.getquery.pagesize"
      layout=" prev,pager, next,sizes,  jumper"
      :total="total">
    </el-pagination>

  <!-- 对话框 -->
     <el-dialog
      :title="fromtitle"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
     <!-- <el-form ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="学科名称" prop="propsubjectName" >
           <el-input v-model="getquery.subjectName" placeholder="请输入学科名称"></el-input>
        </el-form-item>
     </el-form> -->
     <el-form :model="getquery" ref="dateForm" label-width="100px" :rules="rules"  class="demo-ruleForm">
  <el-form-item
   prop="subjectName"
    label="学科名称"
  >
    <el-input type="age" v-model="getquery.subjectName" placeholder="请输入学科名称"></el-input>
  </el-form-item>
<!-- 滑块开关 -->
  <el-form-item prop="isFrontDisplay" label="是否显示">
        <el-switch
        style="display: block"
        :active-value="1"
        :inactive-value="0"
        v-model="isFrontDisplay"
        active-color="#13ce66"
        inactive-color="#ff4949"
      >
       </el-switch>

  </el-form-item>
  </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addform()">确 定</el-button>
       </span>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { list, add, remove, detail, update } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      data: [],
      pages: 0,
      title: '',
      total: 0,
      id: '',
      dialogVisible: false,
      isFrontDisplay: 1,
      searchName: '',

      getquery: {
        pagesize: 10,
        page: 1,
        subjectName: ''
      },
      rules: {
        subjectName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]

      },
      fromtitle: '新增学科'
    }
  },
  created () {
    this.getList()
  },
  computed: {
    isFrontDis: {
      get () {
        return this.data.isFrontDisplay
      },
      set (val) {
        return val === 1
      }

    }
  },
  methods: {
    async getList () {
      const { data } = await list(this.getquery)
      this.title = `数据一共${data.counts}条`
      this.data = data.items
      console.log(this.data)
      this.total = data.counts
    },
    // 清除和搜索
    async search () {
      const { data } = await list({ pagesize: this.getquery.pagesize, page: this.getquery.page, subjectName: this.searchName })
      console.log(data)
      this.data = data.items
    },
    clear () {
      this.searchName = ''
      this.getList()
    },
    // 分页器
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.getquery.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      console.log(val)
      this.getquery.page = val
      this.getList()
    },
    indexMethod (index) {
      return index + 1 + (this.getquery.page - 1) * this.getquery.pagesize
    },
    // 对话框
    handleClose () {
      this.searchName = ''
      this.dialogVisible = false
    },

    // 新增学科-校验
    addform () {
      // 预校验
      this.$refs.dateForm.validate(async valid => {
        if (valid) {
          if (this.fromtitle === '新增学科') {
            const { data } = await add({ isFrontDisplay: this.isFrontDisplay, subjectName: this.getquery.subjectName })
            console.log(data)
            this.id = data
            console.log(this.id)
            this.getquery.page = 1
            this.getList()
            this.getquery.subjectName = ''
            this.dialogVisible = false
          }
          if (this.fromtitle === '修改学科') {
            // 发送修改请求
            await update({ id: this.id, subjectName: this.getquery.subjectName, isFrontDisplay: this.isFrontDisplay })
            this.getquery.page = 1
            this.getList()
            this.getquery.subjectName = ''
            this.dialogVisible = false
          }
          this.getquery.subjectName = ''
        }
        // 符合就发送请求
        // else {
        //   const { res } = await add({this.isFrontDisplay,subjectName:this.getquery.subjectName})
        // }
      })
    },
    // 删除
    delremove (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
      }).then(async () => {
        await remove({ id: row })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(row)
    },
    // 修改
    async amend (row) {
      // 回显
      this.fromtitle = '修改学科'
      this.dialogVisible = true
      this.id = row
      const { data } = await detail({ id: this.id })
      console.log(data)
      this.getquery.subjectName = data.subjectName
      this.isFrontDisplay = data.isFrontDisplay
    },
    delremoves () {
      this.data = []
      this.searchName = ''
    }

  }
}
</script>

<style scoped lang='scss'>
// ::v-deep .el-form-item__label{
//   text-align: left;
// }
.fs {
  font-size: 12px;
}
</style>
